function getUrl(){
    var url = window.location.href;
    var path = window.location.pathname;
    var search = window.location.search;
    url = url.replace(path,"/");
    url = url.replace(search,"")
    return url;
}

$(document).ready(function () {

    const url = getUrl();

    const item ={
        "pageNum":1,
        "search":$("#search").val()
    }

    $.ajax({
        url:url+"adminInfo/listInfo",
        data:JSON.stringify(item),
        type: "post",
        contentType: "application/json; charset=utf-8",
        success: function(map) {
            console.log(map);
            const data = map.blockList;
            var maxPage =Number(map.maxPage);
            $("#maxPage").val(maxPage);
            $("#pageNow").val(1);

            if(data=="unLogin"){
                location.href="login.html";
            }else{
                $.each(data, function (i, data) {
                    const mode = 
                    '<tr>'+
                    '<td id="index"></td>'+
                    '<td id="number"></td>'+
                    '<td id="name"></td>'+
                    '<td id="class"></td>'+
                    '<td id="time"></td>'+
                    '<td><button type="button" class="btn btn-primary" ">详情</button></td>'
                    '</tr>'
                    
                    const $obj = $(mode);
                    $obj.find("#index").text(data.index);
                    $obj.find("#number").text(data.number);
                    $obj.find("#name").text(data.name);
                    $obj.find("#class").text(data.class);
                    $obj.find("#time").text(data.time);
                    // 显示内容
                    $("#table_body").append($obj);
                })

            }

            showPageNum();
            
        },
        error:function(){
            console.log("----------")
        }
    })
    
    $("#table_body").on('click','button',function(){
        const id = $(this).parent().siblings("#index").text();
        console.log(id);
        location.href="infoDetail.html?index="+id;
        //getDetail(id);
    });

    $("#page").on('click','#prePage',function(){
        const page = Number($("#pageNow").val())-1;
        $("#pageNow").val(page);
        showPageNum();
        console.log(page);
        listAjax();
    });

    $("#page").on('click','#nextPage',function(){
        const page = Number($("#pageNow").val())+1;
        $("#pageNow").val(page);
        showPageNum();
        console.log(page);
        listAjax();
    });

    $("#page").on('click','#pagination',function(){
        const page = Number($(this).text());
        $("#pageNow").val(page);
        showPageNum();
        console.log(page);
        listAjax();
    });
    
});

$("#toSearch").click(function () {
    $("#pageNow").val(1);
    showPageNum();
    listAjax();
    console.log($("#search").val());
});

function showPageNum(){
    $("#page").empty();
    const prePage ='<li><a id="prePage">上一页</a></li>';
    const pagination_active = '<li class="active"><a id="pagination"></a></li>';
    const nextPage ='<li><a id="nextPage">下一页</a></li>';
    const $p1 = $(pagination_active);
    const maxPage = $("#maxPage").val();
    const pageNow = $("#pageNow").val();

    if(pageNow>1){
        $("#page").append(prePage);
    }

    for(var i=1;i<=maxPage;i++){
        const pagination ='<li><a id="pagination"></a></li>';
        const $p = $(pagination);
        if(i==pageNow){
            $p1.find("#pagination").text(i);
            $("#page").append($p1);
        }else{
            $p.find("#pagination").text(i);
            $("#page").append($p);
        }
    }

    if(pageNow<maxPage){
        $("#page").append(nextPage);
    }
}

function listAjax(){
    const url = getUrl();

    const item ={
        "pageNum":$("#pageNow").val(),
        "search":$("#search").val()
    }

    $.ajax({
        url:url+"adminInfo/listInfo",
        data:JSON.stringify(item),
        type: "post",
        contentType: "application/json; charset=utf-8",
        success: function(map) {
            console.log(map);
            const data = map.blockList;
            var maxPage =Number(map.maxPage);
            $("#maxPage").val(maxPage);

            if(data=="unLogin"){
                location.href="login.html";
            }else{
                $("#table_body").empty();
                $.each(data, function (i, data) {
                    const mode = 
                    '<tr>'+
                    '<td id="index"></td>'+
                    '<td id="number"></td>'+
                    '<td id="name"></td>'+
                    '<td id="class"></td>'+
                    '<td id="time"></td>'+
                    '<td><button type="button" class="btn btn-primary" ">详情</button></td>'
                    '</tr>'
                    
                    const $obj = $(mode);
                    $obj.find("#index").text(data.index);
                    $obj.find("#number").text(data.number);
                    $obj.find("#name").text(data.name);
                    $obj.find("#class").text(data.class);
                    $obj.find("#time").text(data.time);
                    // 显示内容
                    $("#table_body").append($obj);
                })

            }

            showPageNum();
            
        },
        error:function(){
            console.log("----------")
        }
    })
}